<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      /*
      x = -10 arr[i]
      比较：5>2,2>1,1>-10,-10>8
      交换：arr[3]=arr[0]  [5, 2, 1, 5, 8]
      交换：arr[0]=x  [-10, 2, 1, 5, 8]
      [-10, 2, 1, 5, 8]


      x = 1
      比较：2>1,1>5,1>8
      交换：arr[2]=arr[1]  [5, 2, 2, 5, 8]
      交换：arr[1]=x  [-10, 1, 2, 5, 8]
      [-10, 1, 2, 5, 8]

      */
      let arr = [5, 2, 1, -10, 8];
      // 冒泡算法
      for (let i = 0; i < arr.length; i++) {
        let x = arr[i];
        for (let j = i + 1; j < arr.length; j++) {
          if (x > arr[j]) {
            x = arr[j];
            // 交换
            arr[j] = arr[i];
            arr[i] = x;
          }
        }
      }

      console.log(arr); // 8, 5, 2, 1, -10
    </script>
  </body>
</html>
